import ora, {oraPromise} from 'ora';


const spinner = ora('Loading unicorns').start();
//https://www.npmjs.com/package/ora#spinners
// Name of one of the provided spinners. See example.js in this repo if you want to test out different spinners. On Windows, it will always use the line spinner as the Windows command-line doesn't have proper Unicode support.
// spinner.spinner = 'hamburger'
// ↑ 虽然ora内置的一些spinner在windows中用不了
//
// 但可以像↓这样自定义
spinner.spinner = {
  interval: 80, // Optional
  //frames: ['-', '+', '-']
  frames: [
    "⠋",
    "⠙",
    "⠹",
    "⠸",
    "⠼",
    "⠴",
    "⠦",
    "⠧",
    "⠇",
    "⠏"
  ]
}
//参考 ↓
//https://github.com/sindresorhus/cli-spinners/blob/main/spinners.json
// ↑ emmm 说的是windows中用不了，但你把这里的复制到我们这里还是能用


//region usercase1
// spinner.prefixText = '会出现在loading前面'
// setTimeout(() => {
//   spinner.color = 'yellow';
//   spinner.text = 'Loading rainbows';
// }, 1000);
//
// setTimeout(() => {
//   spinner.color = 'red';
//   spinner.text = 'Loading rainbows';
// }, 2000);
//
// setTimeout(() => {
//   spinner.color = 'blue';
//   spinner.text = 'Loading rainbows';
// }, 3000);
//
// setTimeout(() => {
//   spinner.color = 'magenta';
//   spinner.text = 'Loading rainbows';
// }, 4000);
//
// setTimeout(()=>{
//   spinner.stop()
// },1000*10)
//endregion


//region usercase2 --- 一个下载显示进度的例子
// let percent = 0;
// spinner.text = 'Loading ' + percent + '%';
// spinner.prefixText = 'Download chalk';
//
// const task = setInterval(()=>{
//   percent += 10
//   spinner.text = 'Loading ' + percent + '%';
//   if (percent === 100) {
//     spinner.stop();
//     spinner.prefixText = "";
//     //会留下一行日志
//     spinner.succeed('Download finish!');
//     clearInterval(task);
//   }
// },350)

//endregion

//region usercase3 --- oraPromise
spinner.stop();
//使用 oraPromise 的话就不要像我们上面那样 `const spinner = ora('Loading unicorns').start()` 调用了

;(async function(){
  const promise = new Promise((resolve, reject) => {
    console.log('doing something ...');
    setTimeout(() => {
      resolve();
    }, 2000);
  })
  await oraPromise(promise,{
    successText: 'success@!',
    failText: 'fail@!',
    prefixText: 'Download ora',
    text: 'Loading'
  })
})();
//endregion
